import { fetch, post } from 'plugins/axios'

export function loadList ({state, commit}) {
  fetch('/publisher/get_today_publish/', {
  }).then((response) => {
    commit('loadListSuccess', response)
  }).catch((error) => {
    alert(error.response)
  })
}

export function loadRollList ({state, commit}) {
  state.rollInterval = setInterval(() => {
    fetch('/publisher/get_roll_publish/', {
    }).then((response) => {
      commit('loadRollSuccess', response)
    }).catch((error) => {
      alert(error.response)
    })
  }, 3000)
}

export function loadGrayList ({state, commit}) {
  state.grayInterval = setInterval(() => {
    fetch('/publisher/get_gray_publish/', {
    }).then((response) => {
      commit('loadGraySuccess', response)
    }).catch((error) => {
      alert(error.response)
    })
  }, 3000)
}

export function doRollPublish ({state, commit}) {
  console.log(state.rollPub.map(item => item.id))
  if (state.rollPub.length > 0) {
    post('/publisher/do_muti_roll_publish/',
      state.rollPub.map(item => item.id)
    ).then((response) => {
      commit('doRollPublishSuccess', response)
    }).catch((error) => {
      alert(error.response.data.error)
    })
  } else {
    alert('请勾选')
  }
}

export function doGrayPublish ({state, commit}) {
  if (state.grayPub.length > 0) {
    post('/publisher/do_gray_publish/',
      state.grayPub.map(item => item.id)
    ).then((response) => {
      commit('doGrayPublishSuccess', response)
    }).catch((error) => {
      alert(error.response.data.error)
    })
  } else {
    alert('请勾选')
  }
}
